package com.hifar.test.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hifar.common.QueryParamBean;

import com.hifar.sys.BusiResponse;
import com.hifar.sys.PageResult;
import com.hifar.sys.SystemUtil;
import com.hifar.test.base.pojo.HfEnvConditions;
import com.hifar.test.base.pojo.HfEnvUnitConditions;
import com.hifar.test.base.pojo.VO.HfEnvUnitConditionsAddVO;
import com.hifar.test.base.pojo.VO.HfEnvUnitConditionsQueryParamVo;
import com.hifar.test.base.pojo.VO.HfEnvUnitConditionsVO;
import com.hifar.test.base.service.IHfEnvConditionsService;
import com.hifar.test.base.service.IHfEnvUnitConditionsService;
import com.tssk.kylx.fw.utils.lang.StringUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhuWeiWei
 * @description TODO
 * @date 2025/1/23 10:19
 */
@RestController
@RequestMapping("/api/busi/test/envUnitConditions")
@Api(tags = "基础数据-项目试验条件")
public class HfEnvUnitConditionsController {
    @Autowired
    private IHfEnvConditionsService conditionsService;
    @Autowired
    private IHfEnvUnitConditionsService unitConditionsService;


    @PostMapping("/add")
    public BusiResponse add(@RequestBody @Validated HfEnvUnitConditionsAddVO addVO) {
        String unitId = addVO.getUnitId();
        List<String> conditionsIdList = addVO.getConditionsIdList();
        List<HfEnvUnitConditions> unitConditionsList = new ArrayList<>();
        for (String conditionsId : conditionsIdList) {
            HfEnvUnitConditions hfEnvUnitConditions = new HfEnvUnitConditions();
            hfEnvUnitConditions.setConditionsId(conditionsId);
            hfEnvUnitConditions.setUnitId(unitId);
            unitConditionsList.add(hfEnvUnitConditions);
        }
        unitConditionsService.saveBatch(unitConditionsList);
        return BusiResponse.OK();
    }

    @PostMapping("/delete")
    public BusiResponse delete(@RequestBody List<String> idList) {
        unitConditionsService.removeByIds(idList);
        return BusiResponse.OK();
    }

    @PostMapping("/listPage")
    public BusiResponse listPage(@RequestBody @Validated HfEnvUnitConditionsQueryParamVo queryParamsVO) {
        QueryParamBean<HfEnvUnitConditionsVO> queryParamBean = new QueryParamBean<>(queryParamsVO);
        queryParamBean.addColumns(HfEnvUnitConditions.class, "uc");
        queryParamBean.addField("unitName", "c");
        QueryWrapper<HfEnvUnitConditionsVO> queryWrapper = queryParamBean.getQueryWrapper();
        String unitId = queryParamsVO.getUnitId();
        if (StringUtils.isNotBlank(unitId)) {
            queryWrapper.eq("uc.unit_id", unitId);
        }
        queryWrapper.eq("uc.del_flag", "0");
        queryWrapper.eq("c.del_flag", "0");
        Page<HfEnvUnitConditionsVO> page = queryParamBean.getPage();
        Page<HfEnvUnitConditionsVO> pageData = unitConditionsService.pageVO(page, queryWrapper);
        PageResult pageResult = SystemUtil.parsePage(page, pageData);
        return BusiResponse.OK(pageResult);
    }

    @PostMapping("/listNotUnitPage")
    public BusiResponse listNotUnitPage(@RequestBody @Validated HfEnvUnitConditionsQueryParamVo queryParamsVO) {
        QueryParamBean<HfEnvConditions> queryParamBean = new QueryParamBean<>(queryParamsVO);
        queryParamBean.addColumns(HfEnvConditions.class, "uc");
        queryParamBean.addField("unitName", "c");
        QueryWrapper queryWrapper = queryParamBean.getQueryWrapper();
        String unitId = queryParamsVO.getUnitId();
        if (StringUtils.isNotBlank(unitId)) {
            queryWrapper.apply(" not exists ( select 1 from hf_env_unit_conditions uc " +
                            " where uc.conditions_id=c.id and uc.unit_id={0} and uc.del_flag={1})",
                    unitId, "0");
        }
        queryWrapper.eq("c.del_flag", "0");
        Page<HfEnvConditions> page = queryParamBean.getPage();
        Page<HfEnvConditions> pageData = conditionsService.myPage(page, queryWrapper);
        PageResult pageResult = SystemUtil.parsePage(page, pageData);
        return BusiResponse.OK(pageResult);
    }

}
